package com.acme.mock;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.UnknownHostException;

public class BankClient {

	Socket requestSocket;
	ObjectOutputStream out;
	ObjectInputStream in;
	String message;

	BankClient() {
	}

	void run() {
		try {
			// 1. creating a socket to connect to the server
			requestSocket = new Socket("localhost", 2004);
			System.out.println("Connected to localhost in port 2004");
			// 2. get Input and Output streams
			out = new ObjectOutputStream(requestSocket.getOutputStream());
			out.flush();
			in = new ObjectInputStream(requestSocket.getInputStream());
			// 3: Communicating with the server
			do {
				try {
					message = (String) in.readObject();
					System.out.println("server>" + message);
					out.writeObject("Hi, Server");
					message = "bye";
					sendMessage(makeQuery());
					String answer = (String) in.readObject();
					System.out.println(answer);
				} catch (ClassNotFoundException classNot) {
					System.err.println("data received in unknown format");
				}
			} while (!message.equals("bye"));
		} catch (UnknownHostException unknownHost) {
			System.err.println("You are trying to connect to an unknown host!");
		} catch (IOException ioException) {
			ioException.printStackTrace();
		} finally {
			// 4: Closing connection
			try {
				in.close();
				out.close();
				requestSocket.close();
			} catch (IOException ioException) {
				ioException.printStackTrace();
			}
		}
	}

	void sendMessage(final Command command) {
		try {
			out.writeObject(command);
			out.flush();
		} catch (IOException ioException) {
			ioException.printStackTrace();
		}
	}

	public static void main(final String args[]) {
		BankClient client = new BankClient();
		client.run();
	}

	private Command makeQuery() throws IOException {
		BufferedReader bfr = new BufferedReader(
				new InputStreamReader(System.in));
		String str;
		Command command = new Command();
		System.out.println("Choose a command: (1/2)");
		System.out.println(" 1. Adding a new client \n 2. Deleting a client");
		str = bfr.readLine();
		if(Integer.valueOf(str) == 1) {
			System.out.println("Adding a new client.");
			command.setWhatToDo("add");
			System.out.println("account type (s/c)");
			str = bfr.readLine();
			command.setAccountType(str);
			System.out.println("balance (double value)");
			str = bfr.readLine();
			double balance = Double.valueOf(str);
			command.setBalance(balance);
			if (command.getAccountType().equals("c")) {
				System.out.println("overdraft (double value)");
				str = bfr.readLine();
				double overdraft = Double.valueOf(str);
				command.setBalance(overdraft);
			}
			System.out.println("name");
			str = bfr.readLine();
			command.setName(str);
			System.out.println("gender (m/f)");
			str = bfr.readLine();
			command.setGender(str);
			System.out.println(command);
		} else {
			System.out.println("Deleting a client.");
			command.setWhatToDo("delete");
			System.out.println("name of client for delete:");
			str = bfr.readLine();
			command.setName(str);
		}
		return command;
	}

}
